package dth.bean;

import java.util.ArrayList;
import java.util.Date;

import javax.faces.bean.ManagedBean;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

import dth.dao.MovieDAO;
import dth.dao.PackageDAO;
import dth.dao.UserDAO;
import dth.dao.UserDAO;
import dth.dao.UserPackageDAO;
import dth.entity.Package;
import dth.entity.User;
import dth.entity.User;
import dth.entity.User;
import dth.entity.UserPackage;
import dth.utils.DthUtil;
import antlr.collections.List;

@ManagedBean(name = "userBean")
public class UserBean {
	private int id = 0;// use to insert delete
	private String name;
	private String cardNumber;
	private String contactNumber;
	private String emailAddress;
	private String address;
	private Date serviceSubcribeDate;
	private int monthsRecharge;
	private java.util.List<Package> listPackages = new ArrayList<Package>();
	private String messInvaildCardNumber;
	private String messInvaildContactNumber;
	private String messInvaildEmail;
	/*
	 * user infor
	 */
	private java.util.List<User> users=new ArrayList<User>();
	public java.util.List<Package> getListPackages() {
		listPackages = new PackageDAO().getListPackage();
		if (id != 0) {
			/*
			 * check nhung package user dang chon
			 */
			UserPackageDAO userPackageDAO = new UserPackageDAO();
			java.util.List<UserPackage> lisUserPackages = userPackageDAO
					.getListUserPackgeByUserId(id);
			if (lisUserPackages != null && lisUserPackages.size() > 0) {
				System.out.println("------list package user size:"
						+ lisUserPackages.size());
				for (Package p : listPackages) {
					for (UserPackage u : lisUserPackages) {
						if (u.getOwnPackage().getId() == p.getId()) {
							System.out.println("------co 1 package");
							p.setSelected(true);// chon package
						}
					}
				}

			} else
				System.out.println("---------list packge user used is null---");
		}
		return listPackages;
	}
		
	public String getMessInvaildEmail() {
		if(emailAddress!=null&&emailAddress.length()>0){
			if(!DthUtil.isVaildEmail(emailAddress)){
				System.out.println("-----email k hop le");
				messInvaildContactNumber="(*) your email is not vaild ";
			}
		}
		return messInvaildContactNumber;
	}

	public void setMessInvaildEmail(String messInvaildEmail) {
		this.messInvaildEmail= messInvaildEmail;
	}

	public void setListPackages(java.util.List<Package> listPackages) {
		this.listPackages = listPackages;
	}
	
	public String getMessInvaildContactNumber() {
		if(contactNumber!=null&&contactNumber.length()>0){
			if(!DthUtil.isVaildPhoneNumber(contactNumber)){
				System.out.println("-----phone number k hop le");
				messInvaildContactNumber="(*) your phone number must be a number 10-11 has digit";
			}
		}
		return messInvaildContactNumber;
	}

	public void setMessInvaildContactNumber(String messInvaildContactNumber) {
		this.messInvaildContactNumber = messInvaildContactNumber;
	}

	
	public String getMessInvaildCardNumber() {
		System.out.println("----check card number:"+cardNumber);
		if(id!=0||cardNumber==null||cardNumber.length()==0){
			return messInvaildCardNumber;
		}
		UserDAO userDAO = new UserDAO();
		User user=userDAO.checkUserByCardId(cardNumber);
		if(user!=null){
			System.out.println("-----card is used");
			messInvaildCardNumber="(*)this card id is used";
			return messInvaildCardNumber;
		}
		
		messInvaildCardNumber="";
		return messInvaildCardNumber;
	}
	public void setMessInvaildCardNumber(String messInvaildCardNumber) {
		this.messInvaildCardNumber = messInvaildCardNumber;
	}
	
	public int getMonthsRecharge() {
		return monthsRecharge;
	}

	public void setMonthsRecharge(int monthsRecharge) {
		this.monthsRecharge = monthsRecharge;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCardNumber() {
		return cardNumber;
	}

	public void setCardNumber(String cardNumber) {
		this.cardNumber = cardNumber;
	}

	public String getContactNumber() {
		return contactNumber;
	}

	public void setContactNumber(String contactNumber) {
		this.contactNumber = contactNumber;
	}

	public String getEmailAddress() {
		return emailAddress;
	}

	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Date getServiceSubcribeDate() {
		return serviceSubcribeDate;
	}

	public void setServiceSubcribeDate(Date serviceSubcribeDate) {
		this.serviceSubcribeDate = serviceSubcribeDate;
	}
    
	public java.util.List<User> getUsers() {
		UserDAO dao=new UserDAO();
		System.out.println("-------user id:"+LoginBean.id);
		users=dao.getUserByID(LoginBean.id);
		return users;
	}

	public void setUsers(java.util.List<User> users) {
		this.users = users;
	}

	public UserBean() {
		System.out.println("---request url:"+DthUtil.getRequestURL());
		if(DthUtil.getRequestURL().contains("user.xhtml")){
			System.out.println("--vao day role:"+LoginBean.roleCode);
			if(LoginBean.roleCode!=1)
				DthUtil.navigateTo("welcome.xhtml");
		}
		
		System.out.println("--user bean");
		Object objectAction = DthUtil.getRequest().getParameter("action");
		System.out.println("----------action:" + objectAction);
		if (objectAction != null) {
			if (objectAction.equals("delete")) {
				Object objectId = DthUtil.getRequest().getParameter(
						"customer_id");
				if (objectId != null)
					id = Integer.parseInt(objectId.toString());
				System.out.println("---id delete:" + id);
			} else if (objectAction.equals("update")) {
				Object objectId = DthUtil.getRequest().getParameter(
						"customer_id");
				System.out.println("----"+objectId);
				if (objectId != null)
					id = Integer.parseInt(objectId.toString());
				System.out.println("---id edit:" + id);
				for (User user : getListUser()) {
					if (user.getCustomerId() == id) {
						this.name = user.getCustomerName();
						this.cardNumber = user.getCardNumber();
						this.contactNumber = user.getContactNumber();
						this.emailAddress = user.getEmailAddress();
						this.address = user.getAddress();
						this.serviceSubcribeDate = user
								.getServiceSubcribeDate();
						this.monthsRecharge=user.getMonthsRecharge();
					}
				}
				System.out.println("-----name:" + name);
			}
		}
	}

	public java.util.List<User> getListUser() {
		UserDAO userDAO = new UserDAO();
		return userDAO.getListUser();
	}

	public void delete() {
		try {
			UserDAO userDAO = new UserDAO();
			int count = userDAO.delete(id);
			if (count > 0)
				DthUtil.showMessage("Thông báo", "Đã xóa thành công");
			else
				DthUtil.showMessage("Thông báo", "Đã xóa thất bại");
		} catch (Exception ex) {
			DthUtil.showMessage("Thông báo", "Có lỗi khi xóa");
		}
	}
	public void updateUserProfile(){
		System.out.println("----vao update user profile id:"+id);
		int count=0;
		UserDAO userDAO = new UserDAO();
		User user1 = new User();
		user1.setCustomerId(id);
		user1.setCustomerName(name);
		user1.setCardNumber(cardNumber);
		user1.setContactNumber(contactNumber);
		user1.setEmailAddress(emailAddress);
		user1.setAddress(address);
		user1.setMonthsRecharge(monthsRecharge);
		java.util.Date date = new java.util.Date();// lay ngay hien tai
		user1.setDateInsert(DthUtil.convertToSqlDate(date));
		user1.setServiceSubcribeDate(DthUtil.convertToSqlDate(serviceSubcribeDate));
		if (id != 0)
			count = userDAO.update(user1);			
		if (count > 0)
			DthUtil.showMessage("Mesage", "Update success");
		else
			DthUtil.showMessage("Message", "Update error");
	}
	public void insert() {
		System.out.println("---id=" + id);
		System.out.println("--------vao insert user");
		UserPackageDAO userPackageDAO = new UserPackageDAO();
		if(messInvaildCardNumber!=null&&messInvaildCardNumber.length()>1){
			DthUtil.showMessage("Message", "Id card is invaild");
			return;
		}
		else if(messInvaildContactNumber!=null&&messInvaildContactNumber.length()>1)
		{
			DthUtil.showMessage("Message", "Contact number is invaild");
			return;
		}
		else if(messInvaildEmail!=null&&messInvaildEmail.length()>1)
		{
			DthUtil.showMessage("Message", "Email id invaild");
			return;
		}
		else if(monthsRecharge==0)
		{
			DthUtil.showMessage("Message", "Months recharge is invaild");
			return;
		}
		UserDAO userDAO = new UserDAO();
		for (Package p : listPackages)
			System.out.println(p.isSelected() + "------ass");
		try {

			User user1 = new User();
			user1.setCustomerId(id);
			user1.setCustomerName(name);
			user1.setCardNumber(cardNumber);
			user1.setContactNumber(contactNumber);
			user1.setEmailAddress(emailAddress);
			user1.setAddress(address);
			user1.setMonthsRecharge(monthsRecharge);
			java.util.Date date = new java.util.Date();// lay ngay hien tai
			user1.setDateInsert(DthUtil.convertToSqlDate(date));
			user1.setServiceSubcribeDate(DthUtil
					.convertToSqlDate(serviceSubcribeDate));
			int count = 0;
			if (id == 0)
				count = userDAO.insert(user1);
			else {
				count = userDAO.update(user1);
				userPackageDAO.deleteByUserId(id);
				System.out.println("------------delete all package of user to reselects---");
			}
			/*
			 * insert package for user
			 */
			
			for (Package p : listPackages) {
				if (p.isSelected()) {
					if (count > 0) {
						System.out.println("-------inserted package id:"
								+ p.getId());
					
						UserPackage userPackage = new UserPackage();
						userPackage.setDateInsert(DthUtil
								.convertToSqlDate(date));
						
						userPackageDAO.insert(userPackage, p, user1);
					}
				}
			}
			if (count > 0)
				DthUtil.showMessage("Thông báo", "Đã cập nhật thành công");
			else
				DthUtil.showMessage("Thông báo", "Đã cập nhật thất bại");
		} catch (Exception ex) {
			System.out.println("---------error:" + ex.getMessage());
			DthUtil.showMessage("Thông báo", "Đã insert thất bại");
		}
	}
}
